package com.jin.lazytool.utils;

import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;

public class ExtractMousePointUtil {


    static Stage stage ;        //切图时候的辅助舞台
    static double start_x;     //切图区域的起始位置x
    static double start_y;     //切图区域的起始位置y
    static double awt_start_x;     //切图区域的起始位置x
    static double awt_start_y;     //切图区域的起始位置y
    static double w;           //切图区域宽
    static double h;           //切图区域高
    static double awt_w;           //切图区域宽
    static double awt_h;           //切图区域高
    static HBox hBox;          //切图区域
    static Label label = new Label();

    public static void show(){

        stage = new Stage();
        Rectangle2D bounds = Screen.getPrimary().getBounds();
        AnchorPane pane = new AnchorPane();
        pane.setStyle("-fx-background-color: green;");
        pane.setOpacity(0.2);

        // 容器添加容器
        Scene scene = new Scene(pane,bounds.getWidth(),bounds.getHeight());
        scene.setFill(null);

        //切图窗口绑定鼠标按下事件
        pane.setOnMousePressed(event -> {
            //清除锚点布局中所有子元素
            pane.getChildren().clear();
            //创建切图区域
            hBox = new HBox();
            //设置背景保证能看到切图区域桌面
            hBox.setBackground(null);
            //设置边框
            hBox.setBorder(new Border(new BorderStroke(Paint.valueOf("#c03700"), BorderStrokeStyle.SOLID,
                    null,new BorderWidths(3))));
            pane.getChildren().add(hBox);
            //记录并设置起始位置
            start_x = event.getSceneX();
            start_y = event.getSceneY();
            awt_start_x = MouseInfo.getPointerInfo().getLocation().x;
            awt_start_y = MouseInfo.getPointerInfo().getLocation().y;
//            AnchorPane.setLeftAnchor(hBox,start_x);
//            AnchorPane.setTopAnchor(hBox,start_y);
        });
        //绑定鼠标按下拖拽的事件
        pane.setOnMouseDragged(event -> {
            //用label记录切图区域的长宽
            label.setAlignment(Pos.CENTER);
            label.setPrefHeight(30);
            label.setPrefWidth(170);
            pane.getChildren().remove(label);
            pane.getChildren().add(label);
            label.setTextFill(Paint.valueOf("#ffffff"));//白色填充
            label.setStyle("-fx-background-color: #000000");//黑背景

            if(event.getSceneX() < start_x){
                AnchorPane.setLeftAnchor(hBox,null);
                AnchorPane.setRightAnchor(hBox,pane.getWidth() - start_x);
            }else{
                AnchorPane.setRightAnchor(hBox,null);
                AnchorPane.setLeftAnchor(hBox,start_x);
            }
            if(event.getSceneY() < start_y){
                AnchorPane.setTopAnchor(hBox,null);
                AnchorPane.setBottomAnchor(hBox,pane.getHeight() - start_y);
            }else{
                AnchorPane.setBottomAnchor(hBox,null);
                AnchorPane.setTopAnchor(hBox,start_y);
            }

            AnchorPane.setLeftAnchor(label,start_x+30);
            AnchorPane.setTopAnchor(label,start_y);
            //计算宽高并且完成切图区域的动态效果
            w = Math.abs(event.getSceneX()-start_x);
            h = Math.abs(event.getSceneY()-start_y);
            //计算宽高并且完成切图区域的动态效果
            awt_w = Math.abs(MouseInfo.getPointerInfo().getLocation().x-awt_start_x);
            awt_h = Math.abs(MouseInfo.getPointerInfo().getLocation().y-awt_start_y);
            hBox.setPrefWidth(w);
            hBox.setPrefHeight(h);
            label.setText("宽："+awt_w+" 高："+awt_h + " \r\n左上角: " + awt_start_x + ", " + awt_start_y);

        });

        //绑定鼠标松开事件
        pane.setOnMouseReleased(event -> {
            //记录最终长宽
            w = Math.abs(event.getSceneX()-start_x);
            h = Math.abs(event.getSceneY()-start_y);
            //添加剪切按钮，并显示在切图区域的底部
            Button b = new Button("复制");
            hBox.setBorder(new Border(new BorderStroke(Paint.valueOf("#c03700"), BorderStrokeStyle.SOLID,
                    null,new BorderWidths(3))));
            hBox.getChildren().add(b);
            hBox.setAlignment(Pos.BOTTOM_RIGHT);
            b.setOnAction(event1 -> {
                String str = label.getText();
                Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
                StringSelection strSel = new StringSelection(str);
                cb.setContents(strSel, null);
                stage.close();
            });
            //为切图按钮绑定切图事件
        });
        stage.initStyle(StageStyle.UNDECORATED);
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.setScene(scene);
        stage.setAlwaysOnTop(true);
        stage.show();
        scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                if(keyEvent.getCode() == KeyCode.ESCAPE){
                    stage.close();
                }
            }
        });
    }
    public static void close(){
        stage.close();
    }

}
